<template>
  <div>
    <el-dialog @close="change" v-model="dialogState" title="编辑员工信息">
      <el-form :inline="true" :model="row">
        <el-form-item label="* 姓名" :label-width="formLabelWidth">
          <el-input v-model="row.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item v-if="mode === '3'" label="* 性别"
                      :label-width="formLabelWidth">
          <el-radio-group v-model="row.sex">
            <el-radio label="男"/>
            <el-radio label="女"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="* 手机号码" :label-width="formLabelWidth">
          <el-input v-model="row.phone" autocomplete="off"/>
        </el-form-item>
        <el-form-item v-if="mode !== '4'" label="* 证件号码" :label-width="formLabelWidth">
          <el-input v-model="row.id" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="* 部门" :label-width="dropLabelWidth">
          <el-select v-model="row.dept" placeholder="请选择部门">
            <el-option v-for="item in depts" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="* 岗位" :label-width="formLabelWidth">
          <el-input v-model="row.post" autocomplete="off"/>
        </el-form-item>
        <el-form-item v-if="mode === '1'" label="* 状态" :label-width="dropLabelWidth">
          <el-select v-model="row.status" placeholder="请选择状态">
            <el-option v-for="item in statusList" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="mode === '3' || mode === '4'"
                      label="* 状态" :label-width="formLabelWidth">
          <el-input v-model="row.status" disabled/>
        </el-form-item>
        <el-form-item v-if="mode === '1' || mode === '4'"
                      label="* 入职时间" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker v-model="row.entryDate"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="选择入职日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item v-if="mode === '2' || mode === '3'"
                      label="* 预计入职"
                      :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker v-model="row.estimatedDate"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="预计入职日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item v-if="mode === '4'"
                      label="* 转正时间" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker v-model="row.formalDate"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="选择转正日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item v-if="mode === '2' || mode === '3'"
                      label="* 出生日期"
                      :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker v-model="row.birth"
                            type="date"
                            value-format="YYYY-MM-DD"
                            placeholder="出生日期">
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <!-- 表单尾部取消和确认按钮 -->
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="change">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {getNowDate, computeBirth, debounce} from "../../../plugins/utils";

  export default {
    name: "MyDialog",
    props: {
      row: {
        type: Object,
        default() {
          return {
            name: '',
            sex: '男',
            phone: '',
            id: '',
            dept: '',
            post: '',
            status: '',
            entryDate: getNowDate(),
            estimatedDate: getNowDate(),
            birth: '2000-01-01',
          }
        }
      },
      depts: {
        type: Array,
        default() {
          return []
        }
      },
      statusList: {
        type: Array,
        default() {
          return []
        }
      },
      dialogState: {
        type: Boolean,
        default() {
          return true;
        }
      },
      formLabelWidth: {
        type: String,
        default() {
          return "90px";
        }
      },
      dropLabelWidth: {
        type: String,
        default() {
          return "90px";
        }
      },
      mode: {
        type: String,
        default() {
          return "1";
        }
      }
    },
    methods: {
      confirm(){
        this.$bus.emit('confirmOperation', this.mode)
      },
      change() {
        this.$bus.emit('changeDialogState', this.mode)
      },
      async getBirth(id) {
        console.log(id);
        this.row.birth = computeBirth(id);
        // console.log(this.row.birth);
      }
    },
    watch: {
      'row.id'(newv, oldv) {
        this.debounce_get(newv);
      }
    },
    created() {
      this.debounce_get = debounce(this.getBirth, 1500);
    },
    destroyed() {
      this.$bus.off('confirm');
      this.$bus.off('change');
    }
  }
</script>

<style scoped>

</style>